import md5 from 'js-md5';
import {$get, $setToken, $post} from '../utils/request';
import {$msg_s, $msg_e} from '../utils/msg';

//登录
export const login = (params) => {
  params.loginPwd = md5(md5(params.loginPwd).split('').reverse().join(''));

  let data = $get('Admin/Login', params);

  data = {
    message: '登录成功',
    success: true,
    token: 'dfjoeug08048tjglnvofug0utgrogt',
  }

  const {message, success, token} = data;

  if(success){
    //登录成功
    sessionStorage.setItem('token', token);
    $setToken();
    $msg_s(message);
    localStorage.setItem('loginId', params.loginId);

    if(params.ckMe){
      //记住我
      localStorage.setItem('loginPwd', params.loginPwd);
    }
  }else{
    //登录失败
    $msg_e(message);
  }

  return success;
}

//自动登录
export const loginAuto = () => {
  const loginId = localStorage.getItem('loginId');
  const loginPwd = localStorage.getItem('loginPwd');

  if(loginId && loginPwd){
    //浏览器缓存中有账号、密码
    const params = {
      loginId,
      loginPwd,
      ckMe: true,
    }
  
    let data = $get('Admin/Login', params);
  
    data = {
      message: '登录成功',
      success: true,
      token: 'dfjoeug08048tjglnvofug0utgrogt',
    }
  
    const {success, token} = data;
  
    if(success){
      //登录成功
      sessionStorage.setItem('token', token);
      $setToken();
    }
  
    return success;
  }else{
    //浏览器缓存中没有账号、密码
    return false;
  }
}

//查询账户列表
export const queryList = (params) => {
  let res = $get('Admin/List', params);

  res = {
    data: [
      {
        id: 1,
        loginId: 'admin',
        name: '张三',
        phone: '13700003333',
        photo: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
        role: {
          roleId: 1,
          roleName: '系统管理员',
        },
      },
      {
        id: 2,
        loginId: 'customer',
        name: '李四',
        phone: '13700001111',
        photo: '',
        role: {
          roleId: 2,
          roleName: '普通管理员',
        },
      },
      {
        id: 3,
        loginId: 'admin-3',
        name: '张三三',
        phone: '13700003344',
        photo: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
        role: {
          roleId: 2,
          roleName: '普通管理员',
        },
      },
      {
        id: 4,
        loginId: 'customer-1',
        name: '李四四',
        phone: '13700001115',
        photo: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
        role: {
          roleId: 2,
          roleName: '普通管理员',
        },
      },
      {
        id: 5,
        loginId: 'customer-2',
        name: '李四五',
        phone: '13700001136',
        photo: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
        role: {
          roleId: 2,
          roleName: '普通管理员',
        },
      },
    ],
    count: 6,
    pageNo: 1,
    pageSize: 5,
  }

  return res;
}

//查询账户详情
export const getAdminDetail = (params) => {
  let res = $get('Admin/GetOne', params);

  /* res = {
    id: 1,
    loginId: 'admin',
    name: '张三',
    phone: '13700003333',
    photo: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
    // photo: '',
    role: {
      roleId: 1,
      roleName: '系统管理员',
    },
  } */

  res = {
    id: 2,
    loginId: 'commonAdmin',
    name: '李四',
    phone: '13700002222',
    photo: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
    // photo: '',
    role: {
      roleId: 2,
      roleName: '普通管理员',
    },
  }

  return res;
}

//添加、编辑账户
export const addEditAccount = (params, isAdd) => {
  const handledParams = Object.assign({}, params);

  if(isAdd && !handledParams.loginId){
    //添加时未填写账号
    $msg_e('请输入账号');
    return false;
  }

  if(isAdd && !handledParams.loginPwd){
    //添加时未填写密码
    $msg_e('请输入密码');
    return false;
  }

  if(isAdd && !handledParams.loginPwd2){
    //添加时未填写确认密码
    $msg_e('请输入确认密码');
    return false;
  }

  if(isAdd && handledParams.loginPwd.length < 6){
    //添加时密码小于6位
    $msg_e('密码不能小于6位');
    return false;
  }

  if(!handledParams.phone){
    //未填写手机号
    $msg_e('请输入手机号');
    return false;
  }

  if(!(/^1\d{10}$/.test(handledParams.phone))){
    //手机号格式不正确
    $msg_e('手机号格式不正确');
    return false;
  }

  if(!handledParams.name){
    //未填写姓名
    $msg_e('请输入姓名');
    return false;
  }

  if(!handledParams.roleId){
    //未选择角色
    $msg_e('请选择角色');
    return false;
  }

  if(isAdd){
    //添加
    handledParams.loginPwd = md5(md5(handledParams.loginPwd).split('').reverse().join(''));
  }

  let data = {}

  if(isAdd){
    //添加
    data = $post('Admin/Add', handledParams);

    data = {
      success: true,
      message: '添加账户成功',
    }
  }else{
    //编辑
    data = $post('Admin/Update', handledParams);

    data = {
      success: true,
      message: '编辑账户成功',
    }
  }

  const {success, message} = data;

  if(success){
    //成功
    $msg_s(message);
  }else{
    //失败
    $msg_e(message);
  }

  return success;
}

//删除账户
export const delAccount = (params) => {
  let data = $post('Admin/Delete', params);

  data = {
    success: true,
    message: '删除账户成功',
  }

  const {success, message} = data;

  if(success){
    //成功
    $msg_s(message);
  }else{
    //失败
    $msg_e(message);
  }

  return success;
}

//修改密码
export const resetPwd = (params) => {
  const handledParams = Object.assign({}, params);

  if(!handledParams.oldLoginPwd){
    //未填写原始密码
    $msg_e('请输入原始密码');
    return false;
  }

  if(!handledParams.newLoginPwd){
    //未填写最新密码
    $msg_e('请输入最新密码');
    return false;
  }

  if(!handledParams.newLoginPwd2){
    //未填写确认密码
    $msg_e('请输入确认密码');
    return false;
  }

  if(handledParams.newLoginPwd.length < 6){
    //密码小于6位
    $msg_e('密码不能小于6位');
    return false;
  }

  handledParams.oldLoginPwd = md5(md5(handledParams.oldLoginPwd).split('').reverse().join(''));
  handledParams.newLoginPwd = md5(md5(handledParams.newLoginPwd).split('').reverse().join(''));

  let data = {}

  data = $post('Admin/ResetPwd', handledParams);

  data = {
    success: true,
    message: '修改密码成功',
  }

  const {success, message} = data;

  if(success){
    //成功
    $msg_s(message);
  }else{
    //失败
    $msg_e(message);
  }

  return success;
}